package gw.com.android.ui.trade;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.Locale;

import gw.com.android.app.AppMain;
import gw.com.android.presenter.RewardPresenter;
import www.com.library.util.DeviceUtil;

/**
 * @author jett
 * @since 2018-03-27.
 */
public class RewardDialog {

    private final Dialog dialog;
    private View vCoinBg;
    private ImageView ivCoin;
    private TextView tvMoney;
    private TextView tvText1;
    private TextView tvText2;
    private TextView tvText3;
    private TextView tvGet;
    private AnimationDrawable ad;
    private RewardListener listener;

    public RewardDialog(Context context) {
        View root = View.inflate(context, R.layout.dialog_reward, null);
        initView(root);
        ViewGroup.LayoutParams contentParam = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        dialog = new Dialog(context, R.style.dialog_no_title);
        //dialog.addContentView(root, params);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(root, contentParam);
    }

    private void initView(View root) {
        root.findViewById(R.id.reward_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        vCoinBg = root.findViewById(R.id.reward_coin_container);
        ivCoin = (ImageView) root.findViewById(R.id.reward_coin);
        Typeface font = Typeface.createFromAsset(root.getContext().getResources().getAssets(), "fonts/DIN_Medium.ttf");
        tvMoney = (TextView) root.findViewById(R.id.reward_money);
        tvMoney.setTypeface(font);
        tvText1 = (TextView) root.findViewById(R.id.reward_text1);
        tvText2 = (TextView) root.findViewById(R.id.reward_text2);
        tvText3 = (TextView) root.findViewById(R.id.reward_text3);
        tvGet = (TextView) root.findViewById(R.id.reward_get);
        tvGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnimation(ivCoin);
                if (listener != null) {
                    listener.onNext(true);
                }
            }
        });
    }

    private void startAnimation(ImageView iv) {
        int duration = 100;
        if (ad == null) {
            ad = new AnimationDrawable();
            ad.addFrame(AppMain.getApp().getResources().getDrawable(R.mipmap.a_pop_bounty2), duration);
            ad.addFrame(AppMain.getApp().getResources().getDrawable(R.mipmap.a_pop_bounty3), duration);
            ad.addFrame(AppMain.getApp().getResources().getDrawable(R.mipmap.a_pop_bounty4), duration);
            ad.addFrame(AppMain.getApp().getResources().getDrawable(R.mipmap.a_pop_bounty5), duration);
            ad.addFrame(AppMain.getApp().getResources().getDrawable(R.mipmap.a_pop_bounty6), duration);
            ad.addFrame(AppMain.getApp().getResources().getDrawable(R.mipmap.a_pop_bounty9), duration);
            ad.setOneShot(false);
        }
        iv.setImageDrawable(null);
        iv.setBackgroundDrawable(ad);
        ad.start();
    }

    public RewardDialog setType(int type) {
        CharSequence text;
        if (type == RewardPresenter.OPEN) {
            text = AppMain.getApp().getString(R.string.reward_open_get);
        } else if (type == RewardPresenter.CLOSE) {
            text = AppMain.getApp().getString(R.string.reward_close_get);
        } else {
            return this;
        }
        if (text.length() >= 5) {
            SpannableString ss = new SpannableString(text);
            ForegroundColorSpan span = new ForegroundColorSpan(0xFFF57D23);
            ss.setSpan(span, 4, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            text = ss;
        }
        tvText1.setText(text);
        return this;
    }

    public void onRewardSuccess(String money, int remainOpenNum, int remainCloseNum, int type) {
        if (dialog != null && dialog.isShowing()) {
            if (ivCoin != null) {
                if (ad != null) {
                    ad.stop();
                    ad = null;
                }
                ivCoin.setBackgroundDrawable(null);
                ivCoin.setImageResource(R.mipmap.a_pop_bounty7);
            }
            if (tvMoney != null) {
                tvMoney.setVisibility(View.VISIBLE);
                tvMoney.setText(money);
            }
            if (tvText1 != null && money != null) {
                String content;
                if (type == RewardPresenter.OPEN){
                    content = String.format(Locale.getDefault(), AppMain.getApp().getString(R.string.reward_get_open_success), money);
                }else{
                    content = String.format(Locale.getDefault(), AppMain.getApp().getString(R.string.reward_get_close_success), money);
                }
                SpannableString ss = new SpannableString(content);
                ForegroundColorSpan span = new ForegroundColorSpan(AppMain.getApp().getResources().getColor(R.color.color_k));
                ss.setSpan(span, 4, 5 + money.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvText1.setText(ss);
            }
            if (tvGet != null) {
                tvGet.setText(R.string.reward_to_trade);
                tvGet.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.onNext(false);
                        }
                    }
                });
            }
            if (remainCloseNum > 0) {
                if (tvText2 != null) {
                    tvText2.setVisibility(View.VISIBLE);
                    tvText2.setTextSize(13);
                    tvText2.setTextColor(AppMain.getApp().getResources().getColor(R.color.color_b));
                    String text = AppMain.getApp().getResources().getString(R.string.reward_description);
                    String minLots = String.valueOf(RewardPresenter.getInstance().getMinOpenLots());
                    text = String.format(Locale.getDefault(), text, minLots, RewardPresenter.getInstance().getTotalCloseNum());
                    tvText2.setText(text);
                }
                if (tvText3 != null) {
                    tvText3.setVisibility(View.VISIBLE);
                    tvText3.setTextSize(13);
                    tvText3.setTextColor(AppMain.getApp().getResources().getColor(R.color.color_v));
                    tvText3.setText(R.string.reward_description2);
                    Drawable drawable = AppMain.getApp().getResources().getDrawable(R.mipmap.a_pop_explain);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    tvText3.setCompoundDrawables(drawable, null, null, null);
                    tvText3.setCompoundDrawablePadding(DeviceUtil.instance().dip2px(5, AppMain.getApp()));
                }
            } else {
                String text;
                if (remainOpenNum > 0) {
                    text = String.format(Locale.getDefault(), AppMain.getApp().getResources().getString(R.string.reward_open_unget), remainOpenNum);
                } else {
                    text = AppMain.getApp().getResources().getString(R.string.reward_get_completed);
                }
                if (tvText2 != null) {
                    tvText2.setVisibility(View.VISIBLE);
                    tvText2.setText(text);
                }
            }
        }
    }

    public void onException(Exception e) {
        if (dialog != null && dialog.isShowing()) {
            if (vCoinBg != null) {
                vCoinBg.setBackgroundDrawable(null);
            }
            if (ivCoin != null) {
                if (ad != null) {
                    ad.stop();
                    ad = null;
                }
                ivCoin.setBackgroundDrawable(null);
                ivCoin.setImageResource(R.mipmap.a_pop_nobounty);
            }
            if (tvText1 != null) {
                tvText1.setText(R.string.reward_get_failed);
            }
            if (tvText2 != null) {
                tvText2.setVisibility(View.VISIBLE);
                tvText2.setText(R.string.reward_ready);
            }
            if (tvText3 != null) {
                tvText3.setVisibility(View.VISIBLE);
                tvText3.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                tvText3.setTextSize(14);
                tvText3.setTextColor(AppMain.getApp().getResources().getColor(R.color.color_k));
                tvText3.setText(R.string.reward_to_activity_page);
                tvText3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.onActivityPage();
                        }
                    }
                });
            }
            if (tvGet != null) {
                tvGet.setVisibility(View.GONE);
            }
        }
    }

    public RewardDialog setListener(RewardListener listener) {
        this.listener = listener;
        return this;
    }

    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public boolean isShowing() {
        return dialog != null && dialog.isShowing();
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    public void setOnDissmissListener(DialogInterface.OnDismissListener listener) {
        if (dialog != null) {
            dialog.setOnDismissListener(listener);
        }
    }

    public interface RewardListener {

        void onNext(boolean isGet);

        void onActivityPage();

    }

}
